// const UPPER_ATACK_PER_LEVEL = 10;
// const UPPER_MAXHP_PER_LEVEL = 100;


/**
 * 用户数据
 * MVC：Model
 */
class User extends EventDispatcher {


    x = 0;
    y = 0;
    bagx = 0;
    bagy = 0;
    private path: { x: number, y: number }[] = [];
    rolePicture = new Image();
    //map2 = mapManager.getMap();

    flag = 0;

    view = new renderer.DisplayObjectContainer();
    bagTools: Tool[] = [];

    bagTools2: Tool[] = [];

    // bagToolsTemp: Tool[] = [];
    picture: renderer.Flash;
    pictureStand: renderer.Flash;
    pictureDown: renderer.Flash;
    pictureRight: renderer.Flash;
    pictureLeft: renderer.Flash;
    pictureUp: renderer.Flash;
    checkBitmap: renderer.Bitmap;
    putBitmap: renderer.Bitmap;

    constructor() {
        super();
        const rolePicture = new Image();
        // roleTexture.src = 'role.png';
        // const role = new renderer.Bitmap(0, 0, roleTexture);

        // this.view.addChild(role);
        rolePicture.src = 'assets/role/stand.png';
        const pictureStand = new renderer.Flash(0, 0, rolePicture, rolePicture);
        this.pictureStand = pictureStand;


        const roleTexture1 = new Image();
        roleTexture1.src = 'assets/role/1.png';

        const roleTexture2 = new Image();
        roleTexture2.src = 'assets/role/2.png';

        const roleTexture3 = new Image();
        roleTexture3.src = 'assets/role/3.png';

        const roleTexture4 = new Image();
        roleTexture4.src = 'assets/role/4.png';

        const roleTexture5 = new Image();
        roleTexture5.src = 'assets/role/5.png';

        const roleTexture6 = new Image();
        roleTexture6.src = 'assets/role/6.png';

        const roleTexture7 = new Image();
        roleTexture7.src = 'assets/role/7.png';

        const roleTexture8 = new Image();
        roleTexture8.src = 'assets/role/8.png';


        const pictureDown = new renderer.Flash(0, 0, roleTexture1, roleTexture2);
        this.pictureDown = pictureDown;

        const pictureRight = new renderer.Flash(0, 0, roleTexture3, roleTexture4)
        this.pictureRight = pictureRight;

        const pictureLeft = new renderer.Flash(0, 0, roleTexture5, roleTexture6)
        this.pictureLeft = pictureLeft;

        const pictureUp = new renderer.Flash(0, 0, roleTexture7, roleTexture8);
        this.pictureUp = pictureUp;

        const ring = toolManager.getToolById(6);
        this.bagTools.push(ring);
        this.bagTools2.push(ring);
        const ringBitmap = new renderer.Bitmap(0, 0, ring.pictureM)
        this.bagToolContainer.addChild(ringBitmap);


        const check = new Image();
        check.src = "button1.png";

        const put = new Image();
        put.src = "button2.png";
        const checkBitmap = new renderer.Bitmap(0, 0, check);
        const putBitmap = new renderer.Bitmap(0, 32, put);
        this.checkBitmap = checkBitmap;
        this.putBitmap = putBitmap;


        this.checkInformation();

    }


    update() {
        if (this.path.length > 0) {

            const nextNode = this.path[0]
            const targetX = nextNode.x * TILE_SIZE;
            const targetY = nextNode.y * TILE_SIZE;

            const diffX = targetX - this.view.x;
            const diffY = targetY - this.view.y;
            const speedX = normalize(diffX, 2);
            const speedY = normalize(diffY, 2);




            if (speedX == 0 && speedY == 0) {
                if (this.view.x == 6 * 64 && this.view.y == 2 * 64) {
                    this.dispatchEvent("spy", 111)
                }
                if (this.view.x == 3 * 64 && this.view.y == 5 * 64) {
                    this.dispatchEvent("die", 111)
                }
                if (this.view.x == 2 * 64 && this.view.y == 5 * 64) {
                    this.dispatchEvent("die", 111)
                }

                const map = mapManager.getMap();

                map.grid.setStartNode(nextNode.x, nextNode.y)

                if (this.flag == 1) {
                    user.view.removeAllChildren();

                    user.view.addChild(user.pictureRight);
                }
                else if (this.flag == 2) {
                    user.view.removeAllChildren();
                    user.view.addChild(user.pictureLeft)

                }
                else if (this.flag == 3) {
                    user.view.removeAllChildren();
                    user.view.addChild(user.pictureUp);
                }
                else if (this.flag == 4) {
                    user.view.removeAllChildren();
                    user.view.addChild(user.pictureDown);
                }





                this.path.shift();
                if (this.path.length == 0) {
                    this.dispatchEvent("goOver", null);
                }

            }
            if (speedX > 0) {
                //移动动画
                user.view.removeAllChildren();

                user.view.addChild(user.pictureRight);
                this.flag = 1;

            }
            else if (speedX < 0) {

                user.view.removeAllChildren();

                user.view.addChild(user.pictureLeft);
                this.flag = 2;
            }
            else if (speedY < 0 && speedX == 0) {
                user.view.removeAllChildren();
                user.view.addChild(user.pictureUp);
                this.flag = 3;
            }
            else if (speedY > 0 && speedX == 0) {
                user.view.removeAllChildren();
                user.view.addChild(user.pictureDown);
                this.flag = 4;
            }

            this.view.x += speedX;
            this.view.y += speedY;
            //console.log(this.view.x, this.view.y, "weizhi")

            //     if (this.flag1==1) {

            //     this.put = 0;
            //     this.flag1=0;
            //     this.map2.toolContainer.removeLastTwoChild()
            //     alert("选择错误请重试")
            // }

        }
        else if (this.path.length == 0) {
            user.view.removeAllChildren();
            user.view.addChild(user.pictureStand);
        }

    }


    goto(path: { x: number, y: number }[]) {
        this.path = path;
    }

    name: string = '';

    // hp: number = 100;


    // get maxHp() {
    //     return this.level * UPPER_MAXHP_PER_LEVEL;
    // }


    private _level = 1;
    get level() {
        return this._level;
    }

    set level(value: number) {
        this._level = value;
        this.dispatchEvent("userchange", "111");
    }

    resetPosition(x: number, y: number) {
        this.view.x = x;
        this.view.y = y;
    }

    pick(item: Tool) {

        this.bagTools.push(item);
        //this.bagToolsTemp.push(item);
        // let index = 0;
        // console.log(`捡起${item.toString()}`);
        this.dispatchEvent("userchange", '111');
        // console.log(item);
        this.putInBag(this.bagTools);
        this.bagTools2 = this.bagTools.slice();
        console.log(this.bagTools)
        //this.packegedContainer = new renderer.DisplayObjectContainer();
        // this.packegedContainer.addChild(item.view);
        // item.view.x=0;
        // item.view.y=index*64;
        // index++;
        // this.addEventListener("click", () => {
        //     this.dispatchEvent('toolinformation', {});
        // });

    }


    index = 0;
    index2 = 0;
    bagToolContainer = new renderer.DisplayObjectContainer();
    bagToolContainerTemp = new renderer.DisplayObjectContainer();
    buttonToolContainer = new renderer.DisplayObjectContainer();
    CheckBigContainer = new renderer.DisplayObjectContainer();




    putInBag(bagTools: Tool[]) {
        this.index = 0;
        this.bagToolContainer.removeAllChildren();
        for (let item of bagTools) {
            const tool = toolManager.getToolById(item.id);
            const bagPicture = new renderer.Bitmap(0, this.index * TILE_SIZE, tool.pictureM);
            console.log(tool.pictureM.src, 12323123123123123123123123123)
            this.bagToolContainer.addChild(bagPicture);
            this.index++;
        }
    }

    checkInformation() {
        this.bagToolContainer.addEventListener("click", (eventData: any) => {

            const x = eventData.localX;
            const y = eventData.localY;
            const row = Math.floor(x / TILE_SIZE);
            const col = Math.floor(y / TILE_SIZE);
            this.buttonToolContainer.x = x;
            this.buttonToolContainer.y = y;
            console.log(row, col)
            this.bagx = row;
            this.bagy = col;
            console.log(x, y, "点击xy")

                this.getInformation()
            
            

        })


    }
    put = 0;
    flag1 = 0;
    flag2 = -1;
    flag2t = 0
    flag3 = 0;
    flagRing = 0;
    flagClick = 0;
    flagPut = 11;
    flagCheck=0
    flagLast=0
    flagBook=0
    flagFour=0
    flagCrystal=0
    getBagtools(i:number){
const bagtool=this.bagTools[i]
return bagtool;
    }
    checkDisplay(eventData: any) {
        user.removeEventListener("keydown", user.checkDisplay)
        console.log(eventData,"123454321")
        console.log(this.bagTools)
        const tool =this.getBagtools(eventData)
        console.log(tool,12345679)

        if(this.flagCheck==0&&eventData!=3&&eventData!=10)
        {
            const Bitmap = new renderer.Bitmap(160, 96, this.bagTools2[eventData-1].pictureL)
            this.CheckBigContainer.addChild(Bitmap)
            this.flagCheck=1;
        }
        else if(this.flagCheck==0&&eventData==3&&this.flagBook==0)
        {
            pool.clear();
            const aside = new GameAsideCommand(5);

            pool.add(aside)
            pool.execute();
            this.flagCheck=1;
            this.flagBook=1
        }
        else if(this.flagCheck==0&&eventData==10){
            const image = new Image();
        
            image.src="introduction.png"
            const Bitmap = new renderer.Bitmap(0, 0, image)
            console.log(image)
            this.CheckBigContainer.addChild(Bitmap)
            this.flagCheck=1;
        }
        else{
            this.CheckBigContainer.removeAllChildren();
            this.flagCheck=0;
        }  
        
    }
    getInformation() {
        const map2 = mapManager.getMap();
        
        
        // this.buttonContainer.removeAllChildren();
        if (this.bagy <= this.index) {
            if (this.bagy <=this.index - 1) {
                this.flag2++;
                
            }
            this.flag2t=this.flag2
            if (this.bagy == 0) {

                this.flag2 = 0;
                }
            if (this.bagy == 1) {

                this.flag2 = 0;
                }
            if (this.bagy == 2) {

                this.flag2 = 0;
                }
            if (this.bagy == 3) {

                this.flag2 = 0;
            }
            // if (this.bagy == 4) {

            //     this.flag2 = 0;
            // }
            console.log(this.bagy,"bagy")
            if(this.flagFour==4){
                this.flag2 = 0;
            }
            if(this.flagLast>this.bagy)
            {
                this.flag2=0;
            }
            if(this.bagy>3){
                this.flagLast=this.bagy
            }
                
            const item1 = this.bagTools[this.bagy + this.flag2];
            console.log(item1);
            console.log("flag2", this.flag2);
            // if (this.bagy == 4) {

            //     this.flag2=this.flag2t-1;
            // }
            if (this.bagy == 0) {

                this.flag2=this.flag2t-1;
                }
            if (this.bagy == 1) {

                this.flag2=this.flag2t-1;
                }
            if (this.bagy == 2) {

                this.flag2=this.flag2t-1;
                }
            if (this.bagy == 3) {

                this.flag2=this.flag2t-1;
            }
            // if(this.put!=3)
            // {
            //     this.buttonToolContainer.addChild(this.checkBitmap);
            //     this.buttonToolContainer.addChild(this.putBitmap);
            // }


            //this.buttonToolContainer.addEventListener("click", (eventData: any) => {

            // const x = eventData.localX;
            // const y = eventData.localY;
            // console.log(x,y,"hhhhh")
            // console.log(item1.id,"zzzz")
            if (item1) {
                if (item1.id == 19 || item1.id == 20 || item1.id == 21 || item1.id == 22) {
                    //if(y<=32&&item1.id==14||item1.id==15||item1.id==16||item1.id==17||item1.id==18){
                    //this.buttonToolContainer.removeAllChildren();

                    item1.view.x = user.view.x;
                    item1.view.y = user.view.y;
                    //alert("111")
                    map2.toolContainer.addChild(item1.view);
                    //alert("put:" + this.put)
                    this.bagToolContainer.removeClickChild(this.bagy);
                    this.bagToolContainer.removeAllChildren()

                    //console.log(this.index);
                    // if(this.bagy<this.index-1){
                    //     this.flag1++
                    // }
                    this.bagTools2.splice(this.bagy, 1)

                    //console.log(this.bagToolsTemp);
                    //  this.bagToolContainer.removeClickChild(this.bagy);
                    // console.log(this.bagy);
                    this.index2 = 0;
                    for (let item of this.bagTools2) {
                        const tool = toolManager.getToolById(item.id);
                        const bagPicture = new renderer.Bitmap(0, this.index2 * TILE_SIZE, tool.pictureM);
                        this.bagToolContainer.addChild(bagPicture);
                        this.index2++;
                        console.log("111:::");
                    }

                    this.put = this.put + 1;

                }
                else if (item1.id == 6&&this.flagRing==0) {
                    pool.clear();
                    const aside = new GameAsideCommand(1);

                    pool.add(aside)
                    pool.execute();
                    console.log("333:::");
                    this.flagRing=1
                }
                else if(item1.id == 18){
                    const map=mapManager.getMap()
                    const index =mapManager.maps.indexOf(map)
                    if(index==7)
                    {
                        pool.clear();
                        const s=new TrueEndingCommand();
                        pool.add(s)
                        pool.execute();
                    }
                    else{
                        pool.clear();
                        const aside = new GameAsideCommand(15);

                    pool.add(aside)
                        pool.execute();
                    }
                }

                // else if(item1.id==8){
                //     this.flagRing=1
                // }
                // else if(y<=32&&item1.id==6){
                //     this.put = this.put + 1;
                // }
                // else if(y<=32&&item1.id==6&&this.flagRing==1){
                //     this.put = this.put + 1;
                // }
                else if(item1.id==23&&this.flagCrystal==0){
                    const map=mapManager.getMap()
                    const index =mapManager.maps.indexOf(map)
                    if(index==2){
                        pool.clear();
                        const command = new TrueChosenCommand();

                        pool.add(command)
                        pool.execute();
                        this.flagCrystal=1
                    }

                }
                else if (this.put >= 2 && item1.id == 8) {

                    this.put = 0;
                    this.flag1 = 0;
                    this.index2 = 0;
                    this.flagRing = 0;
                    map2.toolContainer.removeLastTwoChild()
                    map2.toolContainer.removeLastTwoChild()
                    this.bagToolContainer.removeAllChildren()
                    //this.bagTools2=this.bagTools;
                    
                    //alert("选择错误请重试")
                    if(this.flagFour<4){
                        pool.clear();
                        const aside = new GameAsideCommand(this.flagPut);

                        pool.add(aside)
                        pool.execute();
                        this.flagFour++
                    }
                    
                    this.flagPut++
                    if(this.flagFour==4){
                        console.log("delete")
                        this.bagTools.splice(this.bagTools.length-1,1)
                        this.bagTools.splice(this.bagTools.length-1,1)
                        this.bagTools.splice(this.bagTools.length-1,1)
                        this.bagTools.splice(this.bagTools.length-1,1)
                    }
                    
                    this.bagTools2 = this.bagTools.slice();
                    console.log(this.bagTools2)
                    this.flag2 = -1;
                    for (let item of this.bagTools) {
                        const tool = toolManager.getToolById(item.id);
                        const bagPicture = new renderer.Bitmap(0, this.index2 * TILE_SIZE, tool.pictureM);
                        this.bagToolContainer.addChild(bagPicture);
                        this.index2++;
                        console.log("222:::");
                    }
                }

                console.log("flag3", this.flag3)
                console.log(this.put, item1.id);


                //})







                // pool.clear();
                // map2.addEventListener("click", (eventData: any) => {
                //     if (user.view.x == item.view.x && user.view.y == item.view.y) {
                //         const pick = new PickCommand(item);
                //         pool.add(pick)
                //     }
                // })


            }
        }


    }






    // get attack() {
    //     let itemAttack = 0;
    //     for (let item of this.packagedItems) {
    //         itemAttack += item.attack
    //     }
    //     return this.level * UPPER_ATACK_PER_LEVEL + itemAttack;
    // }

    // toString() {
    //     return `[User: level:${this.level} , attack:${this.attack} , ${this.packagedItems.map((item) => item.toString())}]`
    // }
}


function normalize(a: number, value: number) {
    if (a == 0) {
        return 0;
    }
    else if (a > 0) {
        return value;
    }
    else {
        return -value;
    }
}





document.onkeydown = function (event) {
    if (event.keyCode == 49) {
        user.dispatchEvent("keydown", 1)

    }
    else if (event.keyCode == 50) {
        user.dispatchEvent("keydown", 2)

    }
    else if (event.keyCode == 51) {
        user.dispatchEvent("keydown", 3)

    }
    else if (event.keyCode == 52) {
        user.dispatchEvent("keydown", 4)

    }
    else if (event.keyCode == 53) {
        user.dispatchEvent("keydown", 5)

    }
    else if (event.keyCode == 54) {
        user.dispatchEvent("keydown", 6)

    }
    else if (event.keyCode == 55) {
        user.dispatchEvent("keydown", 7)

    }
    else if (event.keyCode == 56) {
        user.dispatchEvent("keydown", 8)

    }
    else if (event.keyCode == 57) {
        user.dispatchEvent("keydown", 9)

    }
    else if (event.keyCode == 13) {
        user.dispatchEvent("keydown", 10)

    }

    user.addEventListener("keydown", (eventData:number)=>{user.checkDisplay(eventData)}
    )
　　}

